<?php
/**
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 15:44
 */

namespace app\api\model;
use Think\Db;
use think\Model;

class Pavilion extends Model
{
    // 表名
    protected $name = 'pavilion';

    /**
     * 插入数据
     * @param $data
     * @return bool|int|string
     */
    public function insertData($data)
    {
        return $this->insertGetId($data);
    }

    /**
     * 编辑数据
     * @param $data
     * @param $where
     * @return Project
     */
    public function updateBywhere($data,$where){
        $r = $this->where($where)->update($data);
        return $r;
    }

    /**
     * 获取总数
     * @param array $where
     * @return int|string
     * @throws \think\Exception
     */
    public function countWhere($where = []){
        $r = $this->where($where)->count();
        return $r;
    }

    /**
     * 假删
     * @param array $where
     * @return Project
     */
    public function deleteWhere($where = []){
        $r = $this->where($where)->update(['is_delete'=>1]);
        return $r;
    }

    /**
     * 获取详情
     * @param array $where
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findWhere($where = []){
        $r = $this->where($where)->find();
        return $r;
    }

    /**
     * 分页列表
     * @param array $where
     * @param string $order
     * @param int $current
     * @param int $pagesize
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function listWherePage($where = [],$order = 'id desc',$current = 0,$pagesize = 10){
        $r = $this->where($where)->order($order)->limit($current.','.$pagesize)->select();
        return $r;
    }

    /**
     * 查询展会下的展馆列表
     */
    public function getListByExhib($where){
        $list = db('exhibition_pavilion')->alias('a')->field('a.id as join_id,b.*')->join('pavilion b','a.pavilion_id = b.id','left')->where($where)->order('a.id desc')->select();
        return $list;
    }

    /**
     * 删除展馆展会关联记录
     * @param $where
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delByExhib($where){
        return db('exhibition_pavilion')->where($where)->delete();
    }

    /**
     * 新增展会展馆关联记录
     * @param $data
     * @return int|string
     */
    public function addByExhib($data){
        return db('exhibition_pavilion')->insertGetId($data);
    }

    /**
     * 查询展馆关联展会
     * @param  pid 展馆id
     */
    public function findExhibition($pid){
        $r =  Db::table('crm_exhibition_pavilion')->alias('a')
            ->join('exhibition c','a.exhibition_id=c.id','left')
            ->where('a.pavilion_id',$pid)
            ->find();
        return $r;
    }

}